/**
 *
 * 拍照获取图片   appendByCamera
 * 调用 appendByCamera方法即可，
 * 需要修改上传地址的ip:port
 */
var server = "http://120.25.151.57:8080/Manage/Mobile/uploadify";
//server = 'http://192.168.199.243:8080/Manage/Mobile/uploadify';
var files = [];
var gps = '0,0';
var address = '无法获取地点';
var wid = null;
// 上传文件
function upload() {
	var wt = plus.nativeUI.showWaiting("正在定位...");
	wt.setTitle("正在上传照片...");
	if(files.length <= 0) {
		plus.nativeUI.alert("没有添加上传文件！");
		return;
	}
	var uid = plus.storage.getItem('uid');
	var scrit = get_scrit();
	server = server + '?gps=' + gps + '&address=' + address+'&uid='+uid+'&pinxh='+scrit.scrit+'&jinxh='+scrit.time;
	var task = plus.uploader.createUpload(server, {
			method: "POST"
		},
		function(t, status) { //上传完成
			if(status == 200) {
				wt.setTitle("上传成功...\n");
				wt.close();
				mui.alert('打卡成功!','提示');
				var data = eval('(' + t.responseText + ')');
				if(data.status == -999){
					mui.alert("登录超时请重新登录", "提示");
					window.location = 'login.html';
				}
				clearFiles();
				plus.storage.removeItem('work_status');
				plus.storage.setItem('work_status', data.status.toString());
				if(data.status == 1) {
					document.getElementById('btn_uploadimage').src = "image/sbqd.png";
				} else {
					document.getElementById('btn_uploadimage').src = "image/xbqd.png";
				}
			} else {
//				console.log(t.responseText);
				plus.nativeUI.alert("上传失败：" + status);
				wt.close();
			}
		}
	);
	task.addData("uid", getUid());
	for(var i = 0; i < files.length; i++) {
		var f = files[i];
		task.addFile(f.path, {
			key: f.name
		});
		//compressImage(f.path, task);
	}
	task.start();
}
// 拍照添加文件
function appendByCamera() {
	plus.camera.getCamera().captureImage(function(p) {
		//appendFile(p);
		compressImage(p);
	});
}

function appendByGallery() {
	plus.gallery.pick(function(p) {
		compressImage(p);

	});
}
// 添加文件
var index = 1;

function appendFile(p) {
	var gelocationTime = 0;
	files.push({
		name: "uploadkey" + index,
		path: p
	});
	index++;
	var wt = plus.nativeUI.showWaiting("正在定位...");
	wid = plus.geolocation.watchPosition( function ( position ) {
		if(gelocationTime < 1){
			gelocationTime++;
			var str = position.coords.longitude + "," + position.coords.latitude; //经度
			gps = str;			
			wid = null;
			wt.close();
			upload();
		}else{
			plus.geolocation.clearWatch(wid);
		}
	}, function (e) {
		if(gelocationTime < 1){
			gelocationTime++;
			wid = null;
			wt.close();
			upload();
		}else{
			plus.geolocation.clearWatch(wid);
		}
	}, {
		provider: 'amap',
		geocode: false,
		timeout:7000
	});
}

// 产生一个随机数
function getUid() {
	return Math.floor(Math.random() * 10000 + 10000).toString();
}

//压缩图片
function compressImage(path) {
	var dst = '_doc/' + getUid() + '.jpg';
//	console.log("开始压缩");
	plus.zip.compressImage({
			src: path,
			dst: dst,
			quality: 20,
			format: 'jpg'
				//overwrite:true,
		},
		function() {
//			console.log("Compress success!");
			appendFile(dst);
		},
		function(error) {
//			console.log("Compress error!");
		}
	);
}

/**
 * 经纬度
 * @param {Object} position
 */
function getPos() {
	//加工地理坐标
	var wt = plus.nativeUI.showWaiting("正在定位...");
	plus.geolocation.getCurrentPosition(function(position){
		var str = position.coords.longitude + "," + position.coords.latitude; //经度
		gps = str;
//		if(gps==undefined){
//			gps = '0,0';
//		}
		address = position.addresses;
//		if(address==undefined){
//			address = '无法获取地点';
//		}
		wt.close();
		upload();
	}, function(e) {
		wt.close();
		upload();
	}, {
		provider: 'amap',
		geocode: false,
		enableHighAccuracy:true
	});
}

function clearFiles(){
	files = [];
}

function clearLastPosAndUpload(){
	plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(fs) {
		//获取位置缓存的目录
		var str = fs.root.fullPath;
		str.lastIndexOf('/');
	    str = str.substr(0, str.lastIndexOf('/'));
	    str.lastIndexOf('/');
	    str = str.substr(0, str.lastIndexOf('/')+1);
	    str = str + 'cache/newlocationCache/';
	    //获取目录
	    plus.io.resolveLocalFileSystemURL( str, function( entry ) {
			entry.removeRecursively( function ( entry1 ) {
				getPos();
			}, function ( e ) {
				getPos();
			} );
		}, function ( e ) {
			getPos();
//			alert( "Request file system failed: " + e.message );
		});
	}, function(e) {
		getPos();
	});
}